<template>
  <div id="lifeTopSearch">
    <h1 class="con-right-title">
      动态维护
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="动态类型" prop="lifeType">
          <el-select v-model="formInline.lifeType" clearable>
            <el-option
              v-for="item in lifeTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序规则" prop="orderBy">
          <el-select v-model="formInline.orderBy">
            <el-option
              v-for="item in orderByList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除状态" prop="delete">
          <el-select v-model="formInline.delete">
            <el-option
              v-for="item in deleteList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间线" prop="timeLine">
          <el-select v-model="formInline.timeLine">
            <el-option
              v-for="item in timeLineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户属性" prop="userAttribute">
          <el-select v-model="formInline.userAttribute" clearable>
            <el-option
              v-for="item in userAttributeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示方式" prop="displayRule">
          <el-select v-model="formInline.displayRule" clearable>
            <el-option
              v-for="item in displayRuleList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容查询" prop="content">
          <el-input
            v-model="formInline.content"
            placeholder="请根据内容搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="用户搜索" prop="userType">
          <el-select v-model="formInline.userType" placeholder="">
            <el-option
              v-for="item in userTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="userSearchContent">
          <el-input
            v-model="formInline.userSearchContent"
            placeholder="请输入相应的搜索内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="关联活动" prop="activityId">
          <el-select
            v-model="formInline.activityId"
            filterable
            placeholder="请选择关联活动"
            clearable
          >
            <el-option
              v-for="item in showActivList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  sysNormalDisable,
  getWorkListActivities
} from '@utils/dictionariesFunc'
export default {
  name: 'LifeTopSearch',
  msg: '动态管理-动态维护-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          lifeType: '', // 动态类型
          orderBy: '', // 排序规则
          delete: '', // 删除状态
          displayRule: '', // 展示方式
          content: '', // 内容查询
          timeLine: '', // 时间线
          userAttribute: '', // 用户属性
          userType: 'dpAccount', // 用户搜索
          userSearchContent: '', // 用户搜索对应内容
          activityId: '', //关联活动ID
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      lifeTypeList: [{ dictLabel: '全部', dictValue: '' }], // 动态类型
      orderByList: [], // 排序规则
      deleteList: [], // 删除状态
      displayRuleList: [
        // 展示方式
        { dictLabel: '全部', dictValue: '' }
      ],
      timeLineList: [],
      userAttributeList: [{ dictLabel: '全部', dictValue: '' }], // 用户属性
      userTypeList: [{ dictLabel: '大鹏号', dictValue: 'dpAccount' }], // 用户类型
      showActivList: [], //关联活动下拉框列表
      rules: {
        lifeType: {},
        orderBy: {},
        delete: {},
        displayRule: {},
        content: {},
        timeLine: {},
        userType: {},
        activityId: {},
        userSearchContent: {},
        userAttribute: {},
        createTime: { type: 'array' }
      },
      linkContentType: 'LIFE' //动态:LIFE;作品:WORKS
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    /* 获取数据状态 */
    async getStatusList() {
      // 动态类型
      let data1 = await sysNormalDisable('sys_work_type')
      this.lifeTypeList = this.lifeTypeList.concat(data1)
      // 展示方式
      let data2 = await sysNormalDisable(
        'video_work_display_rule',
        'displayRule'
      )
      this.displayRuleList = this.displayRuleList.concat(data2)
      // 时间线
      let data3 = await sysNormalDisable('sys_life_time')
      this.timeLineList = [...data3]
      // 排序规则
      let data4 = await sysNormalDisable('sys_life_sort_role')
      this.orderByList = [...data4]
      // 用户属性
      let data5 = await sysNormalDisable('sys_life_user_type', 'lifeUserPro')
      this.userAttributeList = this.userAttributeList.concat(data5)
      //删除状态
      let data6 = await sysNormalDisable('sys_life_del_statu')
      this.deleteList = [...data6]
      //活动关联
      let data7 = await getWorkListActivities(this.linkContentType)
      this.showActivList = [{ id: '', name: '全部' }, ...data7]
      //设置默认数据
      this.setDefaultValue()
      //触发获取列表
      this.$emit('toSearch')
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.timeLine = this.timeLineList[0].dictValue
      this.formInline.orderBy = this.orderByList[0].dictValue
      this.formInline.delete = this.deleteList[0].dictValue
    }
  }
}
</script>

<style lang="less">
#lifeTopSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }

  label.el-form-item__label {
    width: 70px !important;
  }
}
</style>
